package com.us.kafka;

import java.io.Serializable;
import java.util.Random;
import java.util.UUID;

/**
 * @Author : Jianguo.Jing
 * @Email : jingjianguo@hbshongruiht.com
 * @Deacription : TODO
 * @Date : Created in 2018/4/11 11:05
 * @Modified By :
 **/
public class UniqueCodeUntil implements Serializable {

    private static final long serialVersionUID = 5603782476336109031L;

    /* *
     * @ClassName:  com.us.kafka.UniqueCodeUntil
     * @Description:   根据长度生成唯一字符串
     * @author: Jianguo.Jing
     * @param length  需要生成字符串的长度
     * @return: java.lang.String
     * @date:   2018/4/11 11:19
     */
    public static String getOrderIdByUUId(int length) {
        int machineId = 1;//最大支持1-9个集群机器部署
        int hashCodeV = UUID.randomUUID().toString().hashCode();
        if(hashCodeV < 0) {//有可能是负数
            hashCodeV = - hashCodeV;
        }
        //默认为16位
        if(length<0){
            length = -length;
        }else if(length == 0){
            length = 16;
        }
        // 0 代表前面补充0
        // length-1 代表长度为4
        // d 代表参数为正数型
        String  format = "%0" + (length-1) + "d";

        return machineId + String.format(format, hashCodeV);
    }

    public static String getRandomString(int length){
        Random random = new Random();
        String result="";
        //默认为6位
        if(length<0){
            length = -length;
        }else if(length == 0){
            length = 6;
        }
        for (int i=0;i<length;i++)
        {
            result+=random.nextInt(10);
        }
        return result;
    }
    public static long getRandomBigInteger(){
        Random random = new Random();
        String result=getRandomString(16);
        return Long.parseLong(result);
    }
   /* public static void main(String[] args) {
        System.out.println(getRandomString(6));
    }*/
}
